package com.smartens.boit.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.smartens.boit.R;
import com.smartens.boit.bean.DevicesList;

import java.util.List;

/**
 * @Description 正常设备适配器
 * @Author songtt
 * @Date 2021/3/4
 */

public class NormalAdapter extends RecyclerView.Adapter<NormalAdapter.ViewHolder>{
    private List<DevicesList> mList;
    private OnItemClickListener onItemClickListener;
    private OnControlClickListener onControlClickListener;
    private OnEnergyClickListener onEnergyClickListener;

    public NormalAdapter(List<DevicesList> list) {
       this.mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将我们自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_normal, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHolder holder = new ViewHolder(view);
        //返回这个MyHolder实体
        return holder;
    }

    //通过方法提供的ViewHolder，将数据绑定到ViewHolder中
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.name.setText(mList.get(position).getDevicesName());
        holder.number.setText(mList.get(position).getDevicesNum());

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, holder.getAdapterPosition() + 1);
                }
            }
        });
        holder.control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onControlClickListener != null) {
                    onControlClickListener.onItemClick(v, holder.getAdapterPosition() + 1);
                }
            }
        });
        holder.energy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onEnergyClickListener != null) {
                    onEnergyClickListener.onItemClick(v, holder.getAdapterPosition() + 1);
                }
            }
        });

    }
    //获取数据源总的条数
    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        TextView control;
        TextView energy;
        TextView name;
        TextView number;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.detail);
            control = itemView.findViewById(R.id.control);
            energy = itemView.findViewById(R.id.energy);
            name = itemView.findViewById(R.id.name);
            number = itemView.findViewById(R.id.number);
        }
    }
    /**
     * 供外部调用设置监听
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public void setOnControlClickListener(OnControlClickListener onControlClickListener){
        this.onControlClickListener = onControlClickListener;
    }
    public void setOnEnergyClickListener(OnEnergyClickListener onEnergyClickListener){
        this.onEnergyClickListener = onEnergyClickListener;
    }

    public void getData(List<DevicesList> list) {
        mList = list;
    }


    /**
     * 自定义的接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public interface OnControlClickListener {
        void onItemClick(View view, int position);
    }
    public interface OnEnergyClickListener {
        void onItemClick(View view, int position);
    }


}
